package tools.timezone;

import java.util.Date;
import java.util.TimeZone;

public class DateConvert {
    public static long convert(long time, TimeZone sTZ, TimeZone dTZ) {
        if (sTZ == null)
            throw new IllegalArgumentException("Source time zone cannot be null");
        if (dTZ == null)
            throw new IllegalArgumentException("Destination time zone cannot be null");

        long offset = dTZ.getOffset(time) - sTZ.getOffset(time);

        return time + offset;
    }

    public static void main(String[] args) {
        String[] availableIDs = TimeZone.getAvailableIDs();
        for (String id : availableIDs) {
            System.out.println(id);
        }
        System.out.println("**********************************");
        
        long time = System.currentTimeMillis();
        TimeZone localTZ = TimeZone.getDefault();
        System.out.println("Local Time: " + new Date(time));
        
        TimeZone sydneyTZ = TimeZone.getTimeZone("Australia/Sydney");
        long sydneyTime = DateConvert.convert(time, localTZ, sydneyTZ);
        System.out.println("Sydney Time: " + new Date(sydneyTime));
        
        TimeZone victoriaTZ = TimeZone.getTimeZone("Australia/Victoria");
        long victoriaTime = DateConvert.convert(time, localTZ, victoriaTZ);
        System.out.println("Victoria Time: " + new Date(victoriaTime));
        
        TimeZone nyTZ = TimeZone.getTimeZone("America/New_York");
        long nyTime = DateConvert.convert(time, localTZ, nyTZ);
        System.out.println("New York Time: " + new Date(nyTime));
        
        // Consider "time" is New York time, let us convert it to Victoria time,
        // if there was Daylight Saving Time in Victoria, the offset would be
        // 5 hours plus 11 hours.
        long ny2VictoriaTime = DateConvert.convert(time, nyTZ, victoriaTZ);
        System.out.println("Victoria Time (from New York time): " + new Date(ny2VictoriaTime));
    }
}
